#plotting - outcome averages workplace democracy
#alan yan
#2-9-22

####setup####
#clear environment
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       egg,
       car,
       boot,
       grid,
       gridExtra,
       ggpubr,
       Cairo)

#### FUNCTIONS ####

# ggplot theme
theme_shom_alt <- function (base_size = 16, waffle = FALSE) 
{
  ret <- theme_minimal(base_size = base_size, base_family = "Roboto Condensed"
                       ) + 
    theme(plot.background = element_rect(fill = "#f5f5f2", 
                                         color = NA), 
          panel.background = element_rect(fill = "#f5f5f2",color = NA), 
          legend.background = element_rect(fill = "#f5f5f2",color = NA))
  if (waffle) {
    ret + theme(axis.text = element_blank())
  }
  else {
    ret
  }
}

#plotting 
plot.avg <- function(data) {
  ggplot(data, aes(x = exp.condition, y = statistic)) +
    geom_col() +
    geom_errorbar(aes(ymin = statistic - 1.96*std.error, 
                      ymax = statistic + 1.96*std.error,
                      width = 0.4)) +
    scale_y_continuous(limits = c(0,.8)) +
    geom_hline(yintercept = 0.5, color = "red", linetype = "dashed") +
    facet_wrap(~policy.condition, nrow = 1) +
    theme_shom_alt() +
    theme(axis.text.x = element_text(angle = 45, vjust = .5),
          axis.title.x = element_blank())
}

#bootstrap means
boot.dv <- function(policy.condition, dv) {
  boot.outcome <- function(d, i){
    d2 <- d[i,]
    return(mean(d2[[dv]]))
  }
  
  rbind(boot(dt[dt$policy == policy.condition & dt$treatment == "control",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "cost",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "benefit",], boot.outcome, R = 5000) %>% tidy,
        boot(dt[dt$policy == policy.condition & dt$treatment == "both",], boot.outcome, R = 5000) %>% tidy) %>%
    cbind(c("control", "cost", "benefit", "both"),
          policy.condition,
          dv) %>%
    as.data.frame() %>% 
    rename(
      exp.condition = `c(\"control\", \"cost\", \"benefit\", \"both\")`
    ) %>%
    mutate(
      policy.condition = case_when(
        policy.condition == "codeterm" ~ "Codetermination",
        policy.condition == "elections" ~ "Manager elections",
        policy.condition == "esops" ~ "ESOPs",
        policy.condition == "enviro" ~ "Fracking",
        policy.condition == "parent" ~ "Family leave"
      ) %>% 
        factor(.,
               c("Codetermination",
                 "Manager elections",
                 "ESOPs",
                 "Fracking",
                 "Family leave")),
      exp.condition = case_when(
        exp.condition == "control" ~ "Control",
        exp.condition == "cost" ~ "Cost",
        exp.condition == "benefit" ~ "Benefit",
        exp.condition == "both" ~ "Cost and benefit"
      ) %>%
        factor(.,
               c("Control",
                 "Cost",
                 "Benefit",
                 "Cost and benefit"))
    )
}

#load data
dt <- read_rds("02-framing-experiment/data/clean-data-stacked-pol")

#set seed
set.seed(123456789)

#### **workplace democracy policies averages ####
#### support ####
rbind(
  boot.dv("codeterm", "support"),
  boot.dv("elections", "support"),
  boot.dv("esops", "support")
  ) -> data.support
plot.avg(data.support) +
  ylab("Outcome: \nSupport") +
  theme(axis.text.x = element_blank(),
        strip.text = element_text(size = 16)) -> plot.workplace.support
plot.workplace.support

#### work ####
rbind(
  boot.dv("codeterm", "work"),
  boot.dv("elections", "work"),
  boot.dv("esops", "work")
) -> data.work
plot.avg(data.work) +
  ylab("Outcome: \nWork") +
  theme(axis.text.x = element_blank(),
        strip.background = element_blank(),
        strip.text.x = element_blank()) -> plot.workplace.work

#### power ####
rbind(
  boot.dv("codeterm", "power"),
  boot.dv("elections", "power"),
  boot.dv("esops", "power")
) -> data.power
plot.avg(data.power) +
  ylab("Outcome: \nPower") +
  theme(axis.text.x = element_blank(),
        strip.background = element_blank(),
        strip.text.x = element_blank()) -> plot.workplace.power

#### responsibility ####
rbind(
  boot.dv("codeterm", "resp"),
  boot.dv("elections", "resp"),
  boot.dv("esops", "resp")
) -> data.resp
plot.avg(data.resp) +
  ylab("Outcome: \nResponsibility") +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank()) -> plot.workplace.resp
  
#### combined Figure 6 ####
ggarrange(
  plot.workplace.support,
  plot.workplace.work,
  plot.workplace.power,
  plot.workplace.resp,
  nrow = 4,
  heights = c(1,1,1,1.5)
) %>%
  annotate_figure(
    .,
    bottom = text_grob(label = 
    "Note: There are 2,105 Lucid respondents. We fielded the survey from\nJanuary 19th to February 11th, 2022. We bootstrapped our means with\n5,000 draws and show 95% confidence intervals.",
                       hjust = 0,
    size = 11
                       ),
  ) -> plot.workplace.combined
plot.workplace.combined

ggsave("02-framing-experiment/plots/plot-workplace-outcomes-combined.pdf", 
       dpi = 320, width = 10, height = 12, device = cairo_pdf)

